package com.smartthinking.CICPNL.home.activity;

import java.util.ArrayList;
import java.util.Dictionary;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.smartthinking.CICPNL.R;
import com.smartthinking.CICPNL.R.id;
import com.smartthinking.CICPNL.home.objects.App_ImageLoader;
import com.smartthinking.CICPNL.home.objects.clsDictionary;
import com.smartthinking.CICPNL.home.xml.saxXML;

public class Salones extends Activity {

	public static String QRY_BUSCADOR = " select * from cintermex_salones order by cint_idr ";
	public static ArrayList<Dictionary> Actividades = null;
	public ListView list;
	public ListAdapter adapter;
	public Dialog progressDialog;
	public App_ImageLoader imageLoader;
	private Intent intent;
	public static ArrayList<Dictionary> TraeDatos = null;
	public Integer EVEIDR;
	
	
	
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);

		// Checks the orientation of the screen
		 
	}
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.ui_listview);

	/********************************************************************************
     	Obtiene Id del evento
    */
    
    String strEve = "select eve_idr from eventos where eve_idr=1";
	 
	 Utilities findEve = new Utilities();
	 findEve.qry(this,findEve.escape(strEve));
	 TraeDatos = findEve.Actividades;
	 EVEIDR = Integer.parseInt(TraeDatos.get(0).get("eve_idr").toString());
	 /********************************************************************************/
		
		TextView Titulo = (TextView) this.findViewById(id.tv_listView_Titulo);
		Titulo.setText("Salones");

		// @+id/tv_listview_buscar_titutlo2

		// TextView TituloBuscar = (TextView) this
		// .findViewById(id.tv_listview_buscar_titutlo2);
		// TituloBuscar.setText("Salones");

		final EditText buscar = (EditText) this
				.findViewById(id.et_buscar_buscar);

		ImageView buscador = (ImageView) this.findViewById(id.imageView1);
		buscador.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				QRY_BUSCADOR = "select * from cintermex_salones where (upper(cint_salon) like upper('%"
						+ buscar.getText().toString().toUpperCase()
						+ "%') or upper(cint_salon_en) like upper('%"
						+ buscar.getText().toString().toUpperCase() + "%'))";

				CargarContenido();
			}
		});

		CargarContenido();

	}

	public void voidFinish(View V) {
		finish();

	}

	public void abrirMenuLateral(View V) {
		intent = new Intent(Salones.this, MenuLateral.class);

		startActivity(intent);
	}

	@Override
	public void onBackPressed() {
	}

	private void CargarContenido() {
		// TODO Auto-generated method stub
		new AsyncActividades().execute("contenido");

	}

	private class AsyncActividades extends AsyncTask<String, Void, Integer> {

		protected void onPostExecute(Integer in) {

			list = (ListView) findViewById(R.id.list);
			adapter = new Hoteles_Adapter(Actividades);
			list.setAdapter(adapter);

			list.setTextFilterEnabled(true);

			list.setOnItemClickListener(new OnItemClickListener() {

				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {

					Intent intent = new Intent(Salones.this,
							SalonesDetalle.class);
					//
					//
					clsDictionary.setDctionary(Actividades.get(position));
					//
					startActivity(intent);

					// finish();

				}
			});
			progressDialog.dismiss();

			// abrirSplashBanner();
		}

		protected void onPreExecute() {

			progressDialog = ProgressDialog.show(Salones.this, "",
					"Cargando...");
		}

		@SuppressWarnings("rawtypes")
		protected Integer doInBackground(String... urls) {

			// "select a.* from patrocinadores a inner join patrocinadores_eventos b on a.pat_idr = b.pat_idr where b.eve_idr = %@ order by a.pat_nombre"

			String qry_url = getString(R.string.QRY_URL)
					+ Uri.encode(QRY_BUSCADOR);

			saxXML saxparser = new saxXML(qry_url);

			Actividades = saxparser.parse();

			return null;

		}

	}

	public class Hoteles_Adapter extends BaseAdapter {

		private LayoutInflater inflater = null;
		private ArrayList<Dictionary> data;

		public Hoteles_Adapter(ArrayList<Dictionary> d) {

			data = d;
			inflater = (LayoutInflater) Salones.this
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			imageLoader = new App_ImageLoader(
					Salones.this.getApplicationContext());

		}

		public int getCount() {
			return data.size();
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public class ViewHolder {

			public TextView tvTitulo;
			// public ImageView images;

		}

		public View getView(int position, View convertView, ViewGroup parent) {

			View vi = convertView;
			ViewHolder holder;

			if (convertView == null) {

				vi = inflater.inflate(R.layout.ui_salones, null);

				holder = new ViewHolder();

				holder.tvTitulo = (TextView) vi
						.findViewById(R.id.tv_salones_salon);

				vi.setTag(holder);

			} else
				holder = (ViewHolder) vi.getTag();

			try {
				String Titulo = data.get(position).get("cint_salon").toString()
						+ " (" + data.get(position).get("cint_area").toString()
						+ ")";

				holder.tvTitulo.setText(Titulo);
			} catch (Exception e) {
				String Titulo = data.get(position).get("cint_salon").toString();

				holder.tvTitulo.setText(Titulo);
			}

			return vi;

		}
	}

}
